<template>
  <div class="login-page">
    <!-- 背景装饰元素 -->
    <div class="background-decoration">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>
    
    <!-- 主内容容器 -->
    <div class="content-container">
      <!-- 左侧信息面板 -->
      <div class="info-panel">
        <div class="info-content">
          <h2>欢迎回来</h2>
          <p>加入我们的社区，探索无限可能</p>
          <div class="social-links">
            <a href="#" class="social-link"><i class="fab fa-twitter"></i></a>
            <a href="#" class="social-link"><i class="fab fa-facebook"></i></a>
            <a href="#" class="social-link"><i class="fab fa-instagram"></i></a>
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-container">
        <div class="login-form-wrapper">
          <!-- 登录表单头部 -->
          <div class="login-header">
            <img alt="系统图标" src="../assets/logo.svg" class="app-logo" />
            <h1>用户登录</h1>
            <p>请输入您的账号信息</p>
          </div>
          
          <!-- 登录表单 -->
          <form @submit.prevent="handleLogin" class="login-form">
            <!-- 用户名输入框 -->
            <div class="form-group">
              <label for="username" class="form-label">用户名</label>
              <div class="input-wrapper">
                <i class="fas fa-user input-icon"></i>
                <input
                  type="text"
                  id="username"
                  v-model="username"
                  placeholder="请输入用户名"
                  required
                  class="form-input"
                  autocomplete="username"
                />
              </div>
            </div>
            
            <!-- 密码输入框 -->
            <div class="form-group">
              <div class="label-wrapper">
                <label for="password" class="form-label">密码</label>
                <a href="#" class="forgot-password" @click.prevent="handleForgotPassword">忘记密码？</a>
              </div>
              <div class="input-wrapper">
                <i class="fas fa-lock input-icon"></i>
                <input
                  type="password"
                  id="password"
                  v-model="password"
                  placeholder="请输入密码"
                  required
                  class="form-input"
                  autocomplete="current-password"
                />
              </div>
            </div>
            
            <!-- 错误消息 -->
            <div v-if="errorMessage" class="error-message">
              <i class="fas fa-exclamation-circle error-icon"></i>
              {{ errorMessage }}
            </div>
            
            <!-- 记住我选项 -->
            <div class="remember-me">
              <input type="checkbox" id="remember" class="remember-checkbox">
              <label for="remember" class="remember-label">记住我</label>
            </div>
            
            <!-- 登录按钮 -->
            <button
              type="submit"
              class="login-button"
              :disabled="isLoading"
            >
              <span v-if="isLoading" class="loading-spinner"></span>
              {{ isLoading ? '登录中...' : '登录' }}
            </button>
            
            <!-- 分隔线 -->
            <div class="divider">
              <span>或通过以下方式登录</span>
            </div>
            
            <!-- 社交登录选项 -->
            <div class="social-login">
              <button type="button" class="social-button google-btn" @click="handleSocialLogin('google')">
                <i class="fab fa-google"></i>
                <span>使用Google登录</span>
              </button>
              <button type="button" class="social-button microsoft-btn" @click="handleSocialLogin('microsoft')">
                <i class="fab fa-microsoft"></i>
                <span>使用Microsoft登录</span>
              </button>
            </div>
            
            <!-- 注册账号链接 -->
            <div class="register-link">
              <span>还没有账号？</span>
              <a href="#" @click.prevent="handleRegister">立即注册</a>
            </div>
          </form>
          
          <!-- 页脚版权信息 -->
          <div class="login-footer">
            <p>© 2024 现代管理系统. 保留所有权利.</p>
            <div class="footer-links">
              <a href="#" @click.prevent="handleTerms">服务条款</a>
              <a href="#" @click.prevent="handlePrivacy">隐私政策</a>
              <a href="#" @click.prevent="handleHelp">帮助中心</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import authService from '../services/auth.js';

const router = useRouter();
const username = ref('admin'); // 默认用户名，方便测试
const password = ref('password'); // 默认密码，方便测试
const errorMessage = ref('');
const isLoading = ref(false);

// 如果用户已经登录，直接跳转到主页
if (authService.checkAuth()) {
  router.push('/');
}

// 处理登录逻辑
const handleLogin = async () => {
  errorMessage.value = '';
  isLoading.value = true;
  
  try {
    await authService.login(username.value, password.value);
    // 登录成功后跳转到主页
    router.push('/');
  } catch (error) {
    errorMessage.value = error.message;
    // 添加错误动画效果
    const errorElement = document.querySelector('.error-message');
    if (errorElement) {
      errorElement.classList.add('error-shake');
      setTimeout(() => {
        errorElement.classList.remove('error-shake');
      }, 500);
    }
  } finally {
    isLoading.value = false;
  }
};

// 处理忘记密码
const handleForgotPassword = () => {
  alert('忘记密码功能正在开发中');
};

// 处理注册账号
const handleRegister = () => {
  alert('注册账号功能正在开发中');
};

// 处理社交登录
const handleSocialLogin = (provider) => {
  alert(`${provider} 登录功能正在开发中`);
};

// 处理服务条款
const handleTerms = () => {
  alert('服务条款页面正在开发中');
};

// 处理隐私政策
const handlePrivacy = () => {
  alert('隐私政策页面正在开发中');
};

// 处理帮助中心
const handleHelp = () => {
  alert('帮助中心页面正在开发中');
};
</script>

<style scoped>
/* 引入Font Awesome图标库 */
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');

/* 登录页面容器 */
.login-page {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* 背景装饰元素 */
.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 0;
}

.shape {
  position: absolute;
  border-radius: 50%;
  opacity: 0.3;
}

.shape-1 {
  width: 500px;
  height: 500px;
  background: rgba(255, 255, 255, 0.1);
  top: -250px;
  left: -250px;
  animation: float 15s ease-in-out infinite;
}

.shape-2 {
  width: 400px;
  height: 400px;
  background: rgba(255, 255, 255, 0.15);
  bottom: -200px;
  right: -200px;
  animation: float 20s ease-in-out infinite reverse;
}

.shape-3 {
  width: 300px;
  height: 300px;
  background: rgba(255, 255, 255, 0.1);
  top: 50%;
  right: 10%;
  transform: translateY(-50%);
  animation: float 12s ease-in-out infinite;
}

/* 浮动动画 */
@keyframes float {
  0% {
    transform: translate(0, 0) scale(1);
  }
  50% {
    transform: translate(20px, 20px) scale(1.05);
  }
  100% {
    transform: translate(0, 0) scale(1);
  }
}

/* 内容容器 */
.content-container {
  display: flex;
  max-width: 1200px;
  width: 100%;
  height: 80vh;
  max-height: 600px;
  background: var(--color-background);
  border-radius: 30px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  z-index: 1;
  position: relative;
}

/* 信息面板 */
.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #4361ee 0%, #3a0ca3 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: white;
}

.info-content {
  text-align: center;
  max-width: 300px;
}

.info-content h2 {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 20px;
  line-height: 1.2;
}

.info-content p {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 30px;
  line-height: 1.6;
}

.social-links {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-link {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  transition: all 0.3s ease;
}

.social-link:hover {
  background: white;
  color: #4361ee;
  transform: translateY(-3px);
}

/* 登录容器 */
.login-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.app-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
  animation: logo-pulse 3s ease-in-out infinite;
}

@keyframes logo-pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.login-header h1 {
  font-size: 32px;
  font-weight: 700;
  color: var(--color-heading);
  margin-bottom: 10px;
}

.login-header p {
  color: var(--color-text);
  opacity: 0.7;
  font-size: 16px;
}

/* 登录表单 */
.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 25px;
}

.label-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.form-label {
  display: block;
  font-weight: 600;
  color: var(--color-text);
  font-size: 14px;
  margin-bottom: 8px;
}

.forgot-password {
  color: #4361ee;
  font-size: 13px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #3a0ca3;
  text-decoration: underline;
}

.input-wrapper {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #6b7280;
  font-size: 16px;
}

.form-input {
  width: 100%;
  padding: 14px 16px 14px 48px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: var(--color-background);
  color: var(--color-text);
  font-family: inherit;
}

.form-input:focus {
  outline: none;
  border-color: #4361ee;
  box-shadow: 0 0 0 4px rgba(67, 97, 238, 0.1);
}

.form-input::placeholder {
  color: #9ca3af;
}

/* 错误消息 */
.error-message {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #ef4444;
  font-size: 14px;
  padding: 12px 16px;
  background: rgba(239, 68, 68, 0.1);
  border-radius: 10px;
  margin-bottom: 20px;
  border-left: 4px solid #ef4444;
}

.error-icon {
  font-size: 16px;
}

/* 错误抖动动画 */
.error-shake {
  animation: shake 0.5s ease-in-out;
}

@keyframes shake {
  0%, 100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-5px);
  }
  75% {
    transform: translateX(5px);
  }
}

/* 记住我选项 */
.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 25px;
}

.remember-checkbox {
  width: 18px;
  height: 18px;
  accent-color: #4361ee;
}

.remember-label {
  font-size: 14px;
  color: var(--color-text);
  cursor: pointer;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  padding: 14px 24px;
  background: linear-gradient(135deg, #4361ee 0%, #3a0ca3 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0) 100%
  );
  transform: rotate(45deg);
  transition: opacity 0.5s ease;
  opacity: 0;
}

.login-button:hover:not(:disabled)::before {
  opacity: 1;
  animation: shine 1.5s ease-in-out;
}

@keyframes shine {
  0% {
    transform: translateX(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) rotate(45deg);
  }
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 25px rgba(67, 97, 238, 0.3);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

/* 加载动画 */
.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 分隔线 */
.divider {
  display: flex;
  align-items: center;
  gap: 16px;
  margin: 30px 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e5e7eb;
}

.divider span {
  font-size: 14px;
  color: #9ca3af;
  font-weight: 500;
}

/* 社交登录按钮 */
.social-login {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 30px;
}

.social-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 24px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  background: var(--color-background);
  color: var(--color-text);
  font-family: inherit;
}

.social-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border-color: #4361ee;
}

.google-btn:hover {
  border-color: #db4437;
  color: #db4437;
}

.microsoft-btn:hover {
  border-color: #0078d4;
  color: #0078d4;
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-bottom: 30px;
}

.register-link span {
  font-size: 14px;
  color: var(--color-text);
  opacity: 0.8;
}

.register-link a {
  color: #4361ee;
  font-weight: 600;
  text-decoration: none;
  transition: color 0.3s ease;
  margin-left: 4px;
}

.register-link a:hover {
  color: #3a0ca3;
  text-decoration: underline;
}

/* 登录页脚 */
.login-footer {
  text-align: center;
}

.login-footer p {
  font-size: 13px;
  color: var(--color-text);
  opacity: 0.6;
  margin-bottom: 12px;
}

.footer-links {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.footer-links a {
  font-size: 12px;
  color: #4361ee;
  text-decoration: none;
  transition: color 0.3s ease;
}

.footer-links a:hover {
  color: #3a0ca3;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .content-container {
    flex-direction: column;
    height: auto;
    max-height: none;
  }
  
  .info-panel {
    padding: 40px;
  }
  
  .login-container {
    padding: 40px;
  }
}

@media (max-width: 768px) {
  .login-page {
    padding: 15px;
  }
  
  .content-container {
    border-radius: 20px;
    margin: 20px 0;
  }
  
  .info-panel {
    padding: 30px;
  }
  
  .info-content h2 {
    font-size: 28px;
  }
  
  .login-container {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
  
  .form-input {
    padding: 12px 14px 12px 44px;
    font-size: 15px;
  }
  
  .login-button {
    padding: 12px 20px;
    font-size: 15px;
  }
  
  .social-button {
    padding: 12px 20px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .content-container {
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  }
  
  .info-content h2 {
    font-size: 24px;
  }
  
  .login-header h1 {
    font-size: 22px;
  }
  
  .social-login {
    gap: 10px;
  }
  
  .footer-links {
    flex-wrap: wrap;
    gap: 12px;
  }
}
</style>